/* 
Author: Christian Bailer
Contact address: Christian.Bailer@dfki.de 
Department Augmented Vision DFKI 

                          License Agreement
               For Open Source Computer Vision Library
                       (3-clause BSD License)

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

  * Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.

  * Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.

  * Neither the names of the copyright holders nor the names of the contributors
    may be used to endorse or promote products derived from this software
    without specific prior written permission.

This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are disclaimed.
In no event shall copyright holders or contributors be liable for any direct,
indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused
and on any theory of liability, whether in contract, strict liability,
or tort (including negligence or otherwise) arising in any way out of
the use of this software, even if advised of the possibility of such damage.
*/

#pragma once

#ifndef FFTTOOLS_HPP_
#define FFTTOOLS_HPP_

// opencv
// #include <opencv2/core/core.hpp>
#include "cv_mat.hpp"
#include "dsp_intrinsics.hpp"
//NOTE: FFTW support is still shaky, disabled for now.
/*#ifdef USE_FFTW
#include <fftw3.h>
#endif*/

#include "my_hvx.h"
// namespace FFTTools
// {

class FFT{
public:
    explicit FFT(int np): _np(np){
        int logN = (int)Q6_R_ct0_R(np);
        _twiddles1 = (int32_t *)memalign(128, np*4); // 3*np/8*sizeof(int32_t)
        _twiddles2 = (int32_t *)memalign(128, np*4); // np/2*sizeof(int32_t)
        _twiddles = (int32_t *)memalign(128, np*4); // 3*np/4*sizeof(int32_t)
        qhdsp_fft_gen_twiddles_real_ach(_twiddles1, _twiddles2, np, logN);
        qhdsp_fft_gen_twiddles_complex_ach(_twiddles, np, logN);       
        _tmp1 = (int32_t *)memalign(1024, np*np*sizeof(int32_t));
        _tmp2 = (int32_t *)memalign(1024, np*np*sizeof(int32_t));
    }
    ~FFT() {
        free(_twiddles1); 
        free(_twiddles2); 
        free(_twiddles); 
        free(_tmp1);
        free(_tmp2);
    }
    void fftd_chfq(const hfq15_t *in, chfq_t *out);
    void rifftd_chfq(const chfq_t *in, hfq15_t *out);

    void fftd(const hfq15_t *in, cwfq16_t *out);
    // void cfftd(int32_t *in, int32_t *out);
    // void cifftd(int32_t *in, int32_t *out);
    void rifftd(const cwfq16_t *in, hfq15_t *out);
    wfq16_t scale();
private:
    int _np;
    int32_t *_twiddles1;
    int32_t *_twiddles2;
    int32_t *_twiddles;
    
    int32_t *_tmp1;
    int32_t *_tmp2;

};
// note: only for 64x64 or 128x128
class FFT_HVX{
public:
    explicit FFT_HVX(int np): _np(np){
        int logN = (int)Q6_R_ct0_R(np);
        _twiddles1 = (int32_t *)memalign(128, np*4); // np*sizeof(int32_t)
        _twiddles2 = (int32_t *)memalign(128, np*4); // np/2*sizeof(int32_t)
        qhdsp_hvx_2D_fft_gen_twiddles_real_ach(_twiddles1, _twiddles2, np, logN);
    }
    ~FFT_HVX() {
        free(_twiddles1); 
        free(_twiddles2); 
    }

    void fftd(const hfq15_t *in, cwfq16_t *out) {
        // in: signed Q15, out: Q<2*log2(n)+1>.<31-(2*log2(n)+1)>
        // n=128, out: Q15.16
        // n=64, out: Q13.18
        qhdsp_hvx_acw_r2dfft_ah(in, _np, _twiddles1, _twiddles2, out);
    }
    void rifftd(const cwfq16_t *in, hfq15_t *out) {
        // in: Q<2*log2(n)+1>.<31-(2*log2(n)+1)>, out: signed Q15
        qhdsp_hvx_ah_r2difft_acw(in, _np, _twiddles1, _twiddles2, out);
    }

private:
    int _np;

    int32_t *_twiddles1; // Q1.14
    int32_t *_twiddles2; // Q15
};


class FFT_F32{
public:
    explicit FFT_F32(int np): _np(np) {
        int logN = (int)Q6_R_ct0_R(np);
        _twiddle1 = (float complex *)memalign(8, np*sizeof(float complex)); // 3*np/8*sizeof(float complex)
        _twiddle2 = (float complex *)memalign(8, np*sizeof(float complex)); // np/2*sizeof(float complex)
        _c_twiddle = (float complex *)memalign(8, np*sizeof(float complex)); // 3*np/4*sizeof(float complex)
        qhdsp_fft_gen_twiddles_real_acf(_twiddle1, _twiddle2, np, logN);
        qhdsp_fft_gen_twiddles_complex_acf(_c_twiddle, np, logN);

        _tmp = (float complex*)memalign(1024, np*np*sizeof(float complex));
        _tmp2 = (float complex*)memalign(1024, np*np*sizeof(float complex));
    }
    ~FFT_F32() {
        free(_twiddle1);
        free(_twiddle2);
        free(_c_twiddle);
        free(_tmp);
        free(_tmp2);
    }
    void fftd(float *in, float complex *out);
    void ifftd(float complex *in, float complex *out);

    void cfftd(float complex *in, float complex *out);
    void cifftd(float complex *in, float complex *out);

    void rifftd(float complex *in, float *out);
private:
    int _np;
    float complex * _twiddle1;
    float complex * _twiddle2;
    float complex * _c_twiddle;
    float complex * _tmp;
    float complex * _tmp2;
};

void rearrange_64(CvMat_hfq15 &img);
void rearrange_128(CvMat_hfq15 &img);
void rearrange_f(CvMatF32 &img);

// }
#endif // FFTTOOLS_HPP_